﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using DevelopTool.Bll.DbBlls;
using DevelopTool.Common;
using DevelopTool.Forms.BaseForms;
using DevelopTool.Forms.CommonControls;
using DevelopTool.Forms.LodingForm;
using DevelopTool.Model;
using DevelopTool.Model.CommonModels;
using DevelopTool.Model.DbModels;

namespace DevelopTool.Forms.DbForms.UsersForm
{
    public partial class OracleUsersForm : BaseUIPage
    {
        /// <summary>
        /// 连接信息
        /// </summary>
        private DbInfo dbInfo;

        /// <summary>
        /// Oracle操作类
        /// </summary>
        private OracleDbBll oracleDbBll;

        /// <summary>
        /// 查询条件
        /// </summary>
        private string queryKeyword;

        /// <summary>
        /// DataGridView列信息集合
        /// </summary>
        private List<DataGridColumn> dataGridColumns = new List<DataGridColumn>()
              {
                { new DataGridColumn("USERNAME"){ColumnText="用户名",Width=0,ColumnType=ColumnType.DefaultFilter|ColumnType.RowMain}},
                { new DataGridColumn("USER_ID"){ColumnText="用户id",Width=0}},
                 { new DataGridColumn("TEXT"){ColumnText="创建sql",Width=200,ColumnType=ColumnType.DefaultFilter|ColumnType.LongText|ColumnType.CanClick,ShowFormProperty=new ShowFormProperty(){
                BaseNameColumn="USERNAME"
                }}},
                { new DataGridColumn("ACCOUNT_STATUS"){ColumnText="帐户状态",Width=-1}},
                { new DataGridColumn("LOCK_DATE"){ColumnText="帐户锁定日期",Width=-1}},
                { new DataGridColumn("EXPIRY_DATE"){ColumnText="帐户到期日期",Width=-1}},
                { new DataGridColumn("DEFAULT_TABLESPACE"){ColumnText="缺省表空间",Width=150}},
                { new DataGridColumn("TEMPORARY_TABLESPACE"){ColumnText="临时表的缺省表空间",Width=150}},
                { new DataGridColumn("CREATED"){ColumnText="用户创建日期",Width=-1}},
                { new DataGridColumn("PASSWORD_VERSIONS"){ColumnText="密码验证版本",Width=-1}},
                { new DataGridColumn("PROFILE"){ColumnText="资源配置文件",Width=-1}},
                { new DataGridColumn("EXTERNAL_NAME"){ColumnText="外部名称",Width=-1}},
                { new DataGridColumn("AUTHENTICATION_TYPE"){ColumnText="身份验证机制",Width=-1}},
              };


        public OracleUsersForm(DbInfo dbInfo)
        {
            InitializeComponent();

            this.dbInfo = dbInfo.CloneObj();
        }

        public OracleUsersForm(DbInfo dbInfo,string queryKeyword)
        {
            InitializeComponent();

            this.dbInfo = dbInfo.CloneObj();
            this.queryKeyword = queryKeyword;
        }


        private void OracleUsersForm_Load(object sender,EventArgs e)
        {
            this.oracleDbBll = new OracleDbBll(dbInfo);

            InitDate();

            this.dataDisplayControl.DbInfo = this.dbInfo;
        }

        private void InitDate()
        {
            DataTable dataTable = new DataTable();
            LoadingHelper.ShowLoading("获取用户数据",() =>
            {
                dataTable = oracleDbBll.GetUsers();
            });
            this.dataDisplayControl.BindData(dataTable,this.dataGridColumns,"USERNAME ASC");

            if(!string.IsNullOrEmpty(queryKeyword)) this.dataDisplayControl.Search(queryKeyword);
        }

        private void dataDisplayControl_DataGridViewCellClick(DataGridViewEventArgs dataGridViewEventArgs)
        {
            if(dataGridViewEventArgs.MouseButton == MouseButtons.Left)//判断点的是不是左键
            {
                if(dataGridViewEventArgs.Column.Name == "TEXT" && (dataGridViewEventArgs.Cell.Value == null || string.IsNullOrEmpty(dataGridViewEventArgs.Cell.Value.ToString())))
                {
                    string tableName = dataGridViewEventArgs.Row.Cells["USERNAME"].Value.ToString();//取得表名

                    string ddlText = string.Empty;
                    LoadingHelper.ShowLoading($"获取用户{tableName}创建sql语句",() =>
                    {
                        ddlText = oracleDbBll.GetTypeDDLText("USER",tableName,"");
                    });

                    dataGridViewEventArgs.Cell.Value = ddlText;
                }
            }
        }


        private void dataDisplayControl_DataReLoad(object sender,EventArgs e)
        {
            InitDate();
        }
    }
}
